package com.sanyang.pda.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.sanyang.pda.bean.AddEntryInfoRequest;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.TakeStockListBean;
import com.sanyang.pda.event.EventDate;
import com.sanyang.pda.net.NetUtils;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.dialog.base.BaseDialog;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.util.ToastUtils;
import com.wangshen.projectbase.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ScanGoodsDialog extends BaseDialog {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_good_spec)
    TextView tvGoodSpec;
    @BindView(R.id.tv_num_title)
    TextView tvNumTitle;
    @BindView(R.id.tv_num)
    EditText tvNum;
    @BindView(R.id.tv_goods_code)
    TextView tvGoodsCode;
    @BindView(R.id.iv_scan)
    ScanButView ivScan;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_product_time)
    TextView tvProductTime;
    @BindView(R.id.tv_admire_date)
    TextView tvAdmireDate;
    @BindView(R.id.tv_batch)
    EditText tvBatch;
    private ConfirmClick confirmClick;
    ByBarcodeBean barcodeBean;
    private String en;
    public ScanGoodsDialog(@NonNull Context context, ConfirmClick confirmClick) {
        super(context);
        this.confirmClick = confirmClick;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
    }


    public void show(ByBarcodeBean barcodeBean,String en) {
        super.show();
        this.barcodeBean = barcodeBean;
        this.en=en;
        tvName.setText(barcodeBean.getArticleName());
        tvGoodsCode.setText(barcodeBean.getArticleId());
        tvGoodSpec.setText(barcodeBean.getSpec());
    }

    @Override
    public void initData() {

    }

    @Override
    public int getView() {
        return R.layout.dialog_scan_good;
    }

    @OnClick(R.id.iv_close)
    public void onViewClicked() {
        dismiss();
    }

    @OnClick(R.id.tv_cancel)
    public void onTvCancelClicked() {
        dismiss();
    }

    @OnClick(R.id.tv_confirm)
    public void onTvConfirmClicked() {
        if (TextUtils.isEmpty(tvNum.getText().toString())) {
            ToastUtils.showToast(getContext(), getContext().getResources().getString(R.string.quantity_cannot_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvBatch.getText().toString())) {
            ToastUtils.showToast(getContext(), getContext().getResources().getString(R.string.batch)+getContext().getResources().getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvProductTime.getText().toString())) {
            ToastUtils.showToast(getContext(), getContext().getResources().getString(R.string.product_time)+getContext().getResources().getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvAdmireDate.getText().toString())) {
            ToastUtils.showToast(getContext(), getContext().getResources().getString(R.string.admire_date)+getContext().getResources().getString(R.string.not_be_empty));
            return;
        }
        barcodeBean.setNum(Integer.valueOf(tvNum.getText().toString()));
        barcodeBean.setProductTime(tvProductTime.getText().toString());
        barcodeBean.setExpiringTime(tvAdmireDate.getText().toString());
        barcodeBean.setBatchNo(tvBatch.getText().toString());
        AddEntryInfoRequest addEntryInfoRequest=new AddEntryInfoRequest();
        addEntryInfoRequest.setArticleId(barcodeBean.getArticleId());
        addEntryInfoRequest.setBatchNo(tvBatch.getText().toString());
        addEntryInfoRequest.setSingleNumber(en);
        addEntryInfoRequest.setProductTime(tvProductTime.getText().toString());
        addEntryInfoRequest.setExpiringTime(tvAdmireDate.getText().toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(addEntryInfoRequest));
        NetUtils.geApi().addEntryInfoConfig(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<TakeStockListBean>>() {
                    @Override
                    public void accept(BaseAppEntity<TakeStockListBean> manualPickingBeanBaseAppEntity) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(getContext(), throwable.getMessage());
                    }
                });
        if (confirmClick != null) {
            confirmClick.onConfirmClick(barcodeBean);
            dismiss();
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventDate(EventDate eventDate){
        if (eventDate.getType() == 10){
            tvProductTime.setText(eventDate.getDate());
        }else {
            tvAdmireDate.setText(eventDate.getDate());
        }
    }
    @OnClick(R.id.tv_product_time)
    public void onTvProductTimeClicked() {
        Bundle bundle=new Bundle();
        bundle.putString(StringConfig.ENTRYNO,en);
        bundle.putString(StringConfig.ID,barcodeBean.getArticleId());
        bundle.putInt(StringConfig.TYPE,10);
        ARouter.getInstance().build(AppActivityKey.CALENDARACTIVITY).with(bundle).navigation(getContext());
    }

    @OnClick(R.id.tv_admire_date)
    public void onTvAdmireDateClicked() {
        Bundle bundle=new Bundle();
        bundle.putString(StringConfig.ENTRYNO,en);
        bundle.putString(StringConfig.ID,barcodeBean.getArticleId());
        bundle.putInt(StringConfig.TYPE,20);
        ARouter.getInstance().build(AppActivityKey.CALENDARACTIVITY).with(bundle).navigation(getContext());
    }

    public  interface ConfirmClick {
        void onConfirmClick(ByBarcodeBean underDetectionReuqest);
    }
}
